﻿using DAL;
using Model;
using Model.EasyModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    /// <summary>
    /// 用户角色(单角色)权限业务BLL
    /// </summary>
    public class UserPopedomBLL
    {
        /// <summary>
        /// 根据用户角色编码获取用户权限集合
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public List<UserPopedomInfo> GetUserPopedom(int roleID)
        {
            return new UserPopedomDAL().GetUserPopedom(roleID);
        }

        #region 将用户可见页面权限集合转换为EasyUI tree格式的树节点数据集合

        /// <summary>
        /// 将list数据集合转换为EasyUI tree格式的树节点数据集合
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<TreeNode> ToTreeNodes(List<UserPopedomInfo> list)
        {
            List<TreeNode> listNodes = new List<TreeNode>();
            //生成 树节点时，根据 pid=0的根节点 来生成
            LoadTreeNode(list, listNodes, "0");
            return listNodes;
        }

        /// <summary>
        /// 递归list集合 创建 树节点集合
        /// 无级递归
        /// </summary>
        /// <param name="list">数据库list集合</param>
        /// <param name="listChildrenNode">树节点集合</param>
        /// <param name="pID">父节点ID</param>
        private void LoadTreeNode(List<UserPopedomInfo> list, List<TreeNode> listChildrenNode, string pID)
        {
            foreach (var UserPopedom in list)
            {
                //判断权限ParentID 是否和 传入参数相等
                if (UserPopedom.ShangJiBianMa.ToString().Equals(pID))
                {
                    //将 权限转成 树节点
                    //TreeNode node = UserPopedom.ToTreeNode();
                    TreeNode node = new UserPopedomBLL().ToTreeNode(UserPopedom);
                    //将节点加入到 树子节点集合
                    listChildrenNode.Add(node);

                    //递归 为这个新创建的 树节点找 子节点
                    LoadTreeNode(list, node.children, node.id);
                }
            }
        }

        /// <summary>
        /// 生成TreeNode
        /// 配置easyui tree节点对象的属性
        /// </summary>
        /// <param name="up"></param>
        /// <returns></returns>
        private TreeNode ToTreeNode(UserPopedomInfo up)
        {
            //对象的初始化器直接构造
            TreeNode treeNode = new TreeNode()
            {
                id = up.QuanXianBianMa.ToString(),
                text = up.QuanXianMingCheng,
                state = "open",
                //iconCls = this.ICO,
                Checked = false,
                attributes = new { url = up.YeMianLianJie },  //页面链接
                children = new List<TreeNode>() // 子节点集合
            };
            return treeNode;
        }

        #endregion


    }
}
